#if !defined(AFX_CAMERAVIEW_H__454C0879_6383_4671_B441_C41B6670285D__INCLUDED_)
#define AFX_CAMERAVIEW_H__454C0879_6383_4671_B441_C41B6670285D__INCLUDED_

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000

#include <basetype.h>
#include <stdio.h>
#include <math.h>

#define XStart			230
#define XEnd			550
#define YStart			60
#define YEnd			390

#define BMTYPE          0x4d42
#define Threshold		80
#define ColorLength     2
#define ImageSize		(640*480)
#define FilterNum		6
#define FinalNum		100
#define ResultNum		33
#define FriendlyRobots	5
#define EnemyRobots		5

#define DefaultLeniency 50
#define RoboRadius		25
#define BallRadius		8
#define SearchDist		600

#define PI 3.14159265

typedef struct
{
  UINT8         Blue;
  UINT8         Green;
  UINT8         Red;
}BGRPIX;

typedef struct
{
	int Blue;
	int Green;
	int Red;
	int times;
}COLOR;

struct ELEMENT
{
	int label;
	int size;
	float Blue;
	float Green;
	float Red;
	float x,y;
	bool used;
	int closestMatch;
	int closestDist;
	ELEMENT *parent;
};

struct COLORFILTER
{
	int Blue;
	int Green;
	int Red;
	int leniency;
};

struct OBJECT
{
	int x,y;
	int ID;
	int direction;
	int filterNum;
};

class CCameraView : public CView
{
protected:
        // Variables used for compatible bitmaps in memory
        BITMAPINFO     *m_pBitmapInfo;             // Pointer to Bitmap info
        UINT8          *m_pImage;                  // Pointer to actual image

        UINT16          m_OrgXSize;                // Original XSize
        UINT16          m_OrgYSize;                // Original YSize
        
        UINT16          m_XSize;                   // Actual XSize
        UINT16          m_YSize;                   // Actual YSize

        UINT8           m_ColorMode;               // Our color mode
        UINT8           m_BayerPattern;            // Bayer pattern
        UINT8           m_Y16Invert;               // Invert high/low byte

        DWORD           m_HScrollPos;              // Actual HScroll position
        DWORD           m_HScrollMax;              // Maximum HScroll value     
  
        DWORD           m_VScrollPos;              // Actual VScroll position
        DWORD           m_VScrollMax;              // Maximum VScroll position

        BOOL            m_BkgndVisible;            // BkGnd visible or not
        BOOL            m_ScrollMode;              // Scroll mode active flag
        
        RECT            m_SrcRect;                 // Blit rectangle within Image
        RECT            m_DstRect;                 // Blit rectangle within View

        BOOL            m_EraseBkgnd;              // Erase BKGnd flag
        
        BOOL            m_SelectionEnabled;        // Selection enabled
        BOOL            m_DrawSelection;           // Draw selection or not
        RECT            m_DragRect;                // Drag rectangle
        POINT           m_StartPoint;              // Drag point
        int             m_Msg;                     // Message to post 
        HWND            m_hMsgWnd;                 // Window to post to


		bool fore[256][256][256];						// Huge array! True for foreground false otherwise.
		COLOR *keyColors;				// Color array of colors to map image to
		ELEMENT *linked;				// Linked data to determine which detected objects are contiguous
		ELEMENT *final;					// Each blob's representative element. There should never be more than 100 found. 
		COLORFILTER *filter;			// Filters for the 6 imporant colors
										// 0 = blue (team color) (assumes your team is blue)
										// 1 = yellow (team color) (opponent team)
										// 2 = red/pink (direction color)
										// 3 = light green (ID# color)
										// 4 = orange (ball)
										// 5 = white (lines)
		// The game objects
		OBJECT ball;
		OBJECT *friendlyRobot;
		OBJECT *friendlyDir;
		OBJECT *friendlyID;
		OBJECT *enemyRobot;

protected:
	DECLARE_DYNCREATE(CCameraView)
        void            SetScroller(DWORD Which,int Min,int Max,UINT Page,
                                    int Pos,BOOL Redraw=TRUE);
        void            AdjustBlitRectangles(DWORD XSize,DWORD YSize);
        void            RecalcScrollbars();
        void            InitGrayBitmap();
        void            InitColorBitmap();
        void            EraseBkgnd(CDC *pDC);

// Attribute
public:

// Operationen
public:
	CCameraView(); 

        virtual BOOL    CreateBitmap(UINT32 Width,UINT32 Height,UINT8 ColorMode,
                                     UINT8 BayerPattern=0,UINT8 RawMode=FALSE,
                                     UINT8 Y16Invert=FALSE);
        virtual void   DeleteBitmap();

        virtual BITMAPINFO* GetBitmapInfo()     { return m_pBitmapInfo; }
        virtual UINT8* GetImage()               { return m_pImage; }  
        
        virtual void   ProcessFrame(UINT8 *pData,UINT32 Length,UINT8 Redraw=TRUE);

        virtual void   SetScrollmode(BOOL Enable);
        virtual void   EnableSelection(BOOL Enable,HWND hWnd=NULL,int Msg=0);
        virtual void   Resize(RECT *pSrcRect=NULL,UINT16 NewWidth=0,UINT16 NewHeight=0);

		BOOL recal;
		BOOL processImage;
		CPoint mPoint;
		FILE *comFile;
		int frameCount;
		int* ProcessImage();
		int *result;
		int *pResult; // previous results, stores the last PreviousNum results
		inline int colorDistance(int b1, int g1, int r1, int b2, int g2, int r2);
		inline int UVcolorDistance(int b1, int g1, int r1, int b2, int g2, int r2);
		inline int dist(OBJECT *a, OBJECT *b);
		inline int dist(OBJECT *a, ELEMENT *b);
		inline int angle(OBJECT *a, OBJECT *b);
		inline ELEMENT* Find(ELEMENT *x);
		inline void Union(ELEMENT *x, ELEMENT *y);
		int colorSelectStage;			// Progresses as we select each color with the mouse.
										// 0 = select blue (team color) (assumes your team is blue)
										// 1 = select yellow (team color) (opponent team)
										// 2 = select red/pink (direction color)
										// 3 = select light green (ID# color)
										// 4 = select orange (ball)
										// 5 = select white (lines)


	//{{AFX_VIRTUAL(CCameraView)
	protected:
	virtual void OnDraw(CDC* pDC);
	//}}AFX_VIRTUAL

// Implementierung
protected:
	virtual ~CCameraView();
#ifdef _DEBUG
	virtual void AssertValid() const;
	virtual void Dump(CDumpContext& dc) const;
#endif

protected:
	//{{AFX_MSG(CCameraView)
	afx_msg int OnCreate(LPCREATESTRUCT lpCreateStruct);
	afx_msg BOOL OnEraseBkgnd(CDC* pDC);
	afx_msg void OnHScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar);
	afx_msg void OnVScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar);
	afx_msg void OnSize(UINT nType, int cx, int cy);
	//}}AFX_MSG
	DECLARE_MESSAGE_MAP()
public:
        afx_msg void OnLButtonDown(UINT nFlags, CPoint point);
        afx_msg void OnLButtonUp(UINT nFlags, CPoint point);
        afx_msg void OnMouseMove(UINT nFlags, CPoint point);
        afx_msg void OnRButtonUp(UINT nFlags, CPoint point);
};

/////////////////////////////////////////////////////////////////////////////

//{{AFX_INSERT_LOCATION}}

#endif // AFX_CAMERAVIEW_H__454C0879_6383_4671_B441_C41B6670285D__INCLUDED_
